package com.spzx.order.receiver;

import com.rabbitmq.client.Channel;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.order.service.OrderInfoService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.configuration.SpringDocNativeConfiguration;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * ClassName: OrderReceiver
 * Description:
 *
 * @Author linz
 * @Creat 2025/3/26 8:37
 * @Version 1.00
 */
@Slf4j
@Component
public class OrderReceiver {
    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 监听消息
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_PAYMENT_PAY, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_PAYMENT_PAY, durable = "true"),
            key = MqConst.ROUTING_PAYMENT_PAY
    ))
    @SneakyThrows
    public void testReceive(String orderNo, Message message, Channel channel) {
        Long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("修改订单状态：{}", orderNo);
            orderInfoService.processPaySuccess(orderNo);
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
            channel.basicNack(deliveryTag, false,false);
        }
    }

}
